package pub.dsb.example.rx.core;

import io.reactivex.rxjava3.core.Single;
import io.reactivex.rxjava3.core.SingleEmitter;
import io.reactivex.rxjava3.core.SingleObserver;
import io.reactivex.rxjava3.core.SingleOnSubscribe;
import io.reactivex.rxjava3.disposables.Disposable;

/**
 * <p>
 *
 * </p>
 *
 * @author Yi
 * @version V1.0.0
 * @date 2020/12/24 14:32
 * @modificationHistory=========================逻辑或功能性重大变更记录
 * @modify By: {修改人} 2020/12/24 14:32
 * @modify reason: {方法名}:{原因}
 * ...
 * @since V1.0.0
 */
public class SingleTest {
    /**
     * Single
     *
     * @param args
     */
    public static void main(String[] args) {
        System.out.println("DEMO 1 ----");
        //被观察者
        Single<String> single = Single.create(new SingleOnSubscribe<String>() {
            @Override
            public void subscribe(SingleEmitter<String> e) throws Exception {
                e.onSuccess("test");
                e.onSuccess("test2");//错误写法，重复调用也不会处理，因为只会调用一次
            }
        });
        //订阅观察者SingleObserver
        single.subscribe(new SingleObserver<String>() {
            @Override
            public void onSubscribe(Disposable d) {
                //d.dispose();// 直接中断
                System.out.println("onSubscribe " + d.isDisposed());
            }

            @Override
            public void onSuccess(String s) {
                //相当于onNext和onComplete
                System.out.println("RECIEVE: " + s);
            }

            @Override
            public void onError(Throwable e) {
                System.out.println(e.getMessage());
            }
        });
    }
}
